package com.leetcode.sorts.string;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @Author: WuPeng
 * @Date: 2021/6/1 3:04 下午
 *
 * 150.逆波兰表达式求值
 *
 */
public class EvalRPN_150 {

    public static int evalRPN(String[] tokens) {
        Deque<Integer> deque1 = new LinkedList<>();
        for (int i = 0; i < tokens.length; i++) {
            String t = tokens[i];
            if (t.equals("/") || t.equals("*") || t.equals("+") || t.equals("-")){
                Integer s1 = deque1.pop();
                Integer s2 = deque1.pop();
                switch (t){
                    case "+":
                        deque1.push(s2 + s1);
                        break;
                    case "-":
                        deque1.push(s2 - s1);
                        break;
                    case "*":
                        deque1.push(s2 * s1);
                        break;
                    case "/":
                        deque1.push(s2 / s1);
                        break;
                    default:
                }
            }
            else {
                deque1.push(Integer.parseInt(t));
            }
        }
        return deque1.pop();
    }

    public static void main(String[] args) {
        String[] tokens = {"10","6","9","3","+","-11","*","/","*","17","+","5","+"};
        System.out.println(evalRPN(tokens));
    }

}
